/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.IllegalOrphanException;
import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.MaitriserL;
import java.util.ArrayList;
import java.util.Collection;
import com.applicatioN.entities.Exiger1;
import com.applicatioN.entities.Langue;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class LangueJpaController implements Serializable {

    public LangueJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Langue langue) throws PreexistingEntityException, Exception {
        if (langue.getMaitriserLCollection() == null) {
            langue.setMaitriserLCollection(new ArrayList<MaitriserL>());
        }
        if (langue.getExiger1Collection() == null) {
            langue.setExiger1Collection(new ArrayList<Exiger1>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<MaitriserL> attachedMaitriserLCollection = new ArrayList<MaitriserL>();
            for (MaitriserL maitriserLCollectionMaitriserLToAttach : langue.getMaitriserLCollection()) {
                maitriserLCollectionMaitriserLToAttach = em.getReference(maitriserLCollectionMaitriserLToAttach.getClass(), maitriserLCollectionMaitriserLToAttach.getMaitriserLPK());
                attachedMaitriserLCollection.add(maitriserLCollectionMaitriserLToAttach);
            }
            langue.setMaitriserLCollection(attachedMaitriserLCollection);
            Collection<Exiger1> attachedExiger1Collection = new ArrayList<Exiger1>();
            for (Exiger1 exiger1CollectionExiger1ToAttach : langue.getExiger1Collection()) {
                exiger1CollectionExiger1ToAttach = em.getReference(exiger1CollectionExiger1ToAttach.getClass(), exiger1CollectionExiger1ToAttach.getExiger1PK());
                attachedExiger1Collection.add(exiger1CollectionExiger1ToAttach);
            }
            langue.setExiger1Collection(attachedExiger1Collection);
            em.persist(langue);
            for (MaitriserL maitriserLCollectionMaitriserL : langue.getMaitriserLCollection()) {
                Langue oldLangueOfMaitriserLCollectionMaitriserL = maitriserLCollectionMaitriserL.getLangue();
                maitriserLCollectionMaitriserL.setLangue(langue);
                maitriserLCollectionMaitriserL = em.merge(maitriserLCollectionMaitriserL);
                if (oldLangueOfMaitriserLCollectionMaitriserL != null) {
                    oldLangueOfMaitriserLCollectionMaitriserL.getMaitriserLCollection().remove(maitriserLCollectionMaitriserL);
                    oldLangueOfMaitriserLCollectionMaitriserL = em.merge(oldLangueOfMaitriserLCollectionMaitriserL);
                }
            }
            for (Exiger1 exiger1CollectionExiger1 : langue.getExiger1Collection()) {
                Langue oldLangueOfExiger1CollectionExiger1 = exiger1CollectionExiger1.getLangue();
                exiger1CollectionExiger1.setLangue(langue);
                exiger1CollectionExiger1 = em.merge(exiger1CollectionExiger1);
                if (oldLangueOfExiger1CollectionExiger1 != null) {
                    oldLangueOfExiger1CollectionExiger1.getExiger1Collection().remove(exiger1CollectionExiger1);
                    oldLangueOfExiger1CollectionExiger1 = em.merge(oldLangueOfExiger1CollectionExiger1);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findLangue(langue.getIntitule()) != null) {
                throw new PreexistingEntityException("Langue " + langue + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Langue langue) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Langue persistentLangue = em.find(Langue.class, langue.getIntitule());
            Collection<MaitriserL> maitriserLCollectionOld = persistentLangue.getMaitriserLCollection();
            Collection<MaitriserL> maitriserLCollectionNew = langue.getMaitriserLCollection();
            Collection<Exiger1> exiger1CollectionOld = persistentLangue.getExiger1Collection();
            Collection<Exiger1> exiger1CollectionNew = langue.getExiger1Collection();
            List<String> illegalOrphanMessages = null;
            for (MaitriserL maitriserLCollectionOldMaitriserL : maitriserLCollectionOld) {
                if (!maitriserLCollectionNew.contains(maitriserLCollectionOldMaitriserL)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain MaitriserL " + maitriserLCollectionOldMaitriserL + " since its langue field is not nullable.");
                }
            }
            for (Exiger1 exiger1CollectionOldExiger1 : exiger1CollectionOld) {
                if (!exiger1CollectionNew.contains(exiger1CollectionOldExiger1)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Exiger1 " + exiger1CollectionOldExiger1 + " since its langue field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<MaitriserL> attachedMaitriserLCollectionNew = new ArrayList<MaitriserL>();
            for (MaitriserL maitriserLCollectionNewMaitriserLToAttach : maitriserLCollectionNew) {
                maitriserLCollectionNewMaitriserLToAttach = em.getReference(maitriserLCollectionNewMaitriserLToAttach.getClass(), maitriserLCollectionNewMaitriserLToAttach.getMaitriserLPK());
                attachedMaitriserLCollectionNew.add(maitriserLCollectionNewMaitriserLToAttach);
            }
            maitriserLCollectionNew = attachedMaitriserLCollectionNew;
            langue.setMaitriserLCollection(maitriserLCollectionNew);
            Collection<Exiger1> attachedExiger1CollectionNew = new ArrayList<Exiger1>();
            for (Exiger1 exiger1CollectionNewExiger1ToAttach : exiger1CollectionNew) {
                exiger1CollectionNewExiger1ToAttach = em.getReference(exiger1CollectionNewExiger1ToAttach.getClass(), exiger1CollectionNewExiger1ToAttach.getExiger1PK());
                attachedExiger1CollectionNew.add(exiger1CollectionNewExiger1ToAttach);
            }
            exiger1CollectionNew = attachedExiger1CollectionNew;
            langue.setExiger1Collection(exiger1CollectionNew);
            langue = em.merge(langue);
            for (MaitriserL maitriserLCollectionNewMaitriserL : maitriserLCollectionNew) {
                if (!maitriserLCollectionOld.contains(maitriserLCollectionNewMaitriserL)) {
                    Langue oldLangueOfMaitriserLCollectionNewMaitriserL = maitriserLCollectionNewMaitriserL.getLangue();
                    maitriserLCollectionNewMaitriserL.setLangue(langue);
                    maitriserLCollectionNewMaitriserL = em.merge(maitriserLCollectionNewMaitriserL);
                    if (oldLangueOfMaitriserLCollectionNewMaitriserL != null && !oldLangueOfMaitriserLCollectionNewMaitriserL.equals(langue)) {
                        oldLangueOfMaitriserLCollectionNewMaitriserL.getMaitriserLCollection().remove(maitriserLCollectionNewMaitriserL);
                        oldLangueOfMaitriserLCollectionNewMaitriserL = em.merge(oldLangueOfMaitriserLCollectionNewMaitriserL);
                    }
                }
            }
            for (Exiger1 exiger1CollectionNewExiger1 : exiger1CollectionNew) {
                if (!exiger1CollectionOld.contains(exiger1CollectionNewExiger1)) {
                    Langue oldLangueOfExiger1CollectionNewExiger1 = exiger1CollectionNewExiger1.getLangue();
                    exiger1CollectionNewExiger1.setLangue(langue);
                    exiger1CollectionNewExiger1 = em.merge(exiger1CollectionNewExiger1);
                    if (oldLangueOfExiger1CollectionNewExiger1 != null && !oldLangueOfExiger1CollectionNewExiger1.equals(langue)) {
                        oldLangueOfExiger1CollectionNewExiger1.getExiger1Collection().remove(exiger1CollectionNewExiger1);
                        oldLangueOfExiger1CollectionNewExiger1 = em.merge(oldLangueOfExiger1CollectionNewExiger1);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = langue.getIntitule();
                if (findLangue(id) == null) {
                    throw new NonexistentEntityException("The langue with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Langue langue;
            try {
                langue = em.getReference(Langue.class, id);
                langue.getIntitule();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The langue with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<MaitriserL> maitriserLCollectionOrphanCheck = langue.getMaitriserLCollection();
            for (MaitriserL maitriserLCollectionOrphanCheckMaitriserL : maitriserLCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Langue (" + langue + ") cannot be destroyed since the MaitriserL " + maitriserLCollectionOrphanCheckMaitriserL + " in its maitriserLCollection field has a non-nullable langue field.");
            }
            Collection<Exiger1> exiger1CollectionOrphanCheck = langue.getExiger1Collection();
            for (Exiger1 exiger1CollectionOrphanCheckExiger1 : exiger1CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Langue (" + langue + ") cannot be destroyed since the Exiger1 " + exiger1CollectionOrphanCheckExiger1 + " in its exiger1Collection field has a non-nullable langue field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(langue);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Langue> findLangueEntities() {
        return findLangueEntities(true, -1, -1);
    }

    public List<Langue> findLangueEntities(int maxResults, int firstResult) {
        return findLangueEntities(false, maxResults, firstResult);
    }

    private List<Langue> findLangueEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Langue.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Langue findLangue(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Langue.class, id);
        } finally {
            em.close();
        }
    }

    public int getLangueCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Langue> rt = cq.from(Langue.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
